#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tpf_datasource_base.py 
@version:
@time: 2022/01/05 
"""
from TapClientCore.T01_init_conf.v1_init.init_config import MFA, config, datasource_collection
from TapClientCore.T05_TM_feature.v1.TMF01_datasource.tmf_datasource import TMFDataSource
from TapUtil.decorator.some_useful import timeout, func_logger


class V1TPFDataSourceBase:

    def __init__(self, change=''):
        self.TMFDataSource = TMFDataSource(change)
        self.serve = self.TMFDataSource.v1tm.serve
        self.user = self.TMFDataSource.v1tm.user

    # 数据源test，加载schema，并等待状态为finished
    def _datasource_test(self, connections_name):
        self.TMFDataSource.test_connection_by_name(connections_name)
        self.wait_datasource_status_finished(connections_name)
        self.wait_datasource_status_ready(connections_name)

    # 数据源加载schema，并等待状态为finished
    def _datasource_load_schema(self, connections_name):
        self.TMFDataSource.load_schema_by_name(connections_name)
        self.wait_datasource_status_finished(connections_name)

    # 等待数据源schema状态finished
    @timeout('finished', 200)
    def wait_datasource_status_finished(self, connections_name):
        response = self.TMFDataSource.query_datasource_by_name(connections_name)
        return response.get('loadFieldsStatus')

    # 等待数据状态为ready
    @timeout('ready', 200)
    def wait_datasource_status_ready(self, connections_name):
        response = self.TMFDataSource.query_datasource_by_name(connections_name)
        return response.get('status')

    # insert
    @func_logger
    def datasource_insert_mongodb(self, db_info, connection_id):
        name = db_info.get('name')
        MFA.insert_one(
            datasource_collection,
            {
                'name': name,
                'serve': self.serve,
                'info': db_info,
                'id': connection_id,
                'use': self.user
            }
        )

    # delete
    @func_logger
    def datasource_delete_mongodb(self, db_info):
        name = db_info.get('name')
        MFA.delete_one(
            datasource_collection,
            {
                'name': name,
                'serve': self.serve,
                'user': self.user
            }
        )

    # update
    @func_logger
    def datasource_update_mongodb(self, db_info, connection_id, update=None):
        name = db_info.get('name')
        if update:
            MFA.update(
                datasource_collection,
                {
                    'name': name,
                    'serve': self.serve,
                    'user': self.user
                },
                {
                    '$set': {'id': connection_id,
                             'info': update,
                             'name': update['name']}
                }
            )
        MFA.update(
            datasource_collection,
            {
                'name': name,
                'serve': self.serve,
                'user': self.user
            },
            {
                '$set': {
                    'id': connection_id,
                    'info': db_info
                }
            }
        )

    # query
    @func_logger
    def datasource_query_mongodb(self, db_info):
        name = db_info.get('name')
        query_result = MFA.query(
            datasource_collection,
            {
                'name': name,
                'serve': self.serve,
                'user': self.user
            }
        )
        if query_result:
            return query_result[0]
        return query_result
